package com.hghivln.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hghivln.constants.Constants;
import com.hghivln.mapper.CheckItemMapper;
import com.hghivln.pojo.domain.CheckItem;
import com.hghivln.pojo.dto.CheckItemDto;
import com.hghivln.pojo.vo.AjaxResult;
import com.hghivln.service.CheckItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class CheckItemServiceImpl implements CheckItemService{

    @Autowired
    private CheckItemMapper checkItemMapper;

    @Override
    public AjaxResult listCheckItemForPage(CheckItemDto checkItemDto) {
        Page<CheckItem> page = new Page<>(checkItemDto.getPageNum(), checkItemDto.getPageSize());
        QueryWrapper<CheckItem> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(checkItemDto.getCheckItemName()),CheckItem.COL_CHECK_ITEM_NAME,checkItemDto.getCheckItemName());
        qw.like(StringUtils.isNotBlank(checkItemDto.getKeywords()),CheckItem.COL_KEYWORDS,checkItemDto.getKeywords());
        qw.eq(checkItemDto.getStatus() != null,CheckItem.COL_STATUS,checkItemDto.getStatus());
        qw.eq(StringUtils.isNotBlank(checkItemDto.getTypeId()),CheckItem.COL_TYPE_ID,checkItemDto.getTypeId());
        qw.eq(CheckItem.DEL_FLAG, Constants.DEL_FALSE);
        qw.orderByAsc(CheckItem.COL_CREATE_TIME);
        checkItemMapper.selectPage(page,qw);
        return AjaxResult.success("查询成功",page.getRecords(),page.getTotal());
    }

    @Override
    public AjaxResult addCheckItem(CheckItemDto checkItemDto) {
        CheckItem checkItem = new CheckItem();
        BeanUtils.copyProperties(checkItemDto,checkItem);
        checkItem.setCreateBy(checkItemDto.getSimpleUser().getUserName());
        checkItem.setCreateTime(DateUtil.date());
        return AjaxResult.toAjax(checkItemMapper.insert(checkItem));
    }

    @Override
    public AjaxResult updateCheckItem(CheckItemDto checkItemDto) {
        CheckItem checkItem = new CheckItem();
        BeanUtils.copyProperties(checkItemDto,checkItem);
        checkItem.setUpdateBy(checkItemDto.getSimpleUser().getUserName());
        return AjaxResult.toAjax(checkItemMapper.updateById(checkItem));
    }

    @Override
    public AjaxResult deleteCheckItemByIds(Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        if (idList.size()>0) {
            return AjaxResult.success(checkItemMapper.deleteCheckItemByIds(idList));
        }
        return AjaxResult.fail("删除失败");
    }

    @Override
    public AjaxResult selectCheckItemById(Long id) {
        QueryWrapper<CheckItem> qw = new QueryWrapper<>();
        qw.eq(CheckItem.DEL_FLAG,Constants.DEL_FALSE);
        qw.eq(CheckItem.COL_CHECK_ITEM_ID,id);
        return AjaxResult.success(checkItemMapper.selectOne(qw));
    }

    @Override
    public AjaxResult selectAllCheckItem() {
        QueryWrapper<CheckItem> qw = new QueryWrapper<>();
        qw.eq(CheckItem.DEL_FLAG,Constants.DEL_FALSE);
        qw.eq(CheckItem.COL_STATUS,Constants.STATUS_TRUE);
        return AjaxResult.success(checkItemMapper.selectList(qw));
    }

}
